﻿package util.idgenerator;

import java.net.InetAddress;

//字符串ID生成工厂类
public class StringIDGeneratorFactory {
	private static short counter = (short) 0;

	private static final int JVM = (int) (System.currentTimeMillis() >>> 8);

	private static final String areaCode = ""; // 行政区号,从属性文件中动态获取

	private static final int IP;
	static {
		int ipadd;
		try {
			ipadd = toInt(InetAddress.getLocalHost().getAddress());
		} catch (Exception e) {
			ipadd = 0;
		}
		IP = ipadd;
	}

	private static int toInt(byte[] bytes) {
		int result = 0;
		for (int i = 0; i < 4; i++) {
			result = (result << 8) - Byte.MIN_VALUE + bytes[i];
		}
		return result;
	}

	protected int getIP() {
		return IP;
	}

	/**
	 * Unique across JVMs on this machine (unless they load this class in the
	 * same quater second - very unlikely)
	 */
	protected int getJVM() {
		return JVM;
	}

	/**
	 * Unique in a millisecond for this JVM instance (unless there are >
	 * Short.MAX_VALUE instances created in a millisecond)
	 */
	private static synchronized short getCount() {
		if (counter < 0)
			counter = 0;
		return counter++;
	}

	/**
	 * Unique down to millisecond
	 */
	protected short getHiTime() {
		return (short) (System.currentTimeMillis() >>> 32);
	}

	protected int getLoTime() {
		return (int) System.currentTimeMillis();
	}

	/**
	 * 格式化成8位
	 */
	protected String format(int intval) {
		String formatted = Integer.toHexString(intval);
		StringBuffer buf = new StringBuffer("00000000");
		buf.replace(8 - formatted.length(), 8, formatted);
		return buf.toString();
	}

	/**
	 * 格式化成4位
	 */
	protected String format(short shortval) {
		String formatted = Integer.toHexString(shortval);
		StringBuffer buf = new StringBuffer("0000");
		buf.replace(4 - formatted.length(), 4, formatted);
		return buf.toString();
	}

	/**
	 * 行政区号 6位
	 */
	protected String getAreaCode() {
		return areaCode;
	}

	/**
	 * 以行政区号为前缀 如果行政区号没有设置 则以IP为前缀
	 */
	protected String getPrefix() {
		String prefix = getAreaCode();
		if (prefix == null || prefix.equals("")) {
			prefix = format(getIP());
		} else {
			if (prefix.length() > 8) {
				prefix = prefix.substring(0, 8);
			}
		}
		return prefix;
	}

	/**
	 * 生成ID
	 */
	public String generateID() {
		return new StringBuffer(32).append(getPrefix())
				.append(format(getJVM())).append(format(getHiTime()))
				.append(format(getLoTime())).append(format(getCount()))
				.toString();
	}

	public static void main(String[] args) {
		String id = new StringIDGeneratorFactory().generateID();
		System.out.println(id + "的长度:" + id.length());
	}

}
